package com.longway.common.safe;
 
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

import com.jfinal.kit.StrKit;
/**
* 重写HttpServletRequestWrapper用于配合EdecryInterceptor
*/
public class EdecryHttpServletRequestWrapper extends javax.servlet.http.HttpServletRequestWrapper{
	
	   private Map<String,String[]> params;//定义参数集合
	 
	   // 重新赋值的body数据
       private String bodyJsonStr;

	    public EdecryHttpServletRequestWrapper(HttpServletRequest request,String bodyJsonStr) {
	        super(request);
	        this.bodyJsonStr = bodyJsonStr;
	        this.params = new HashMap<String, String[]>();
	    }
	  
	    //需要一个request和 篡改之后的参数进行实例化。
	    public EdecryHttpServletRequestWrapper(HttpServletRequest request, Map<String,String[]> newParams) {
	        super(request);
	        this.params = newParams;
	    }

	    //查找自定的Map进行返回
	    @Override
	    public String getParameter(String name) {
	        Object v = params.get(name);
	        if (v == null) {
	            return null;
	        } else if (v instanceof String[]) { //一个name可能对应多个value， 返回第一个
	            String[] strArr = (String[]) v;
	            if (strArr.length > 0) {
	                return strArr[0]; 
	            } else {
	                return null;
	            }
	        } else if (v instanceof String) {
	            return (String) v;
	        } else {
	            return v.toString();
	        }
	    }

	    @Override
	    public Map getParameterMap() {
	        return params;
	    }

	    @Override
	    public Enumeration getParameterNames() {
	        return new Vector(params.keySet()).elements();
	    }

	    @Override
	    public String[] getParameterValues(String name) {
	        Object v = params.get(name);
	        if (v == null) {
	            return null;
	        } else if (v instanceof String[]) {
	            return (String[]) v;
	        } else if (v instanceof String) {
	            return new String[] { (String) v };
	        } else {
	            return new String[] { v.toString() };
	        }
	    }
	    
	    @Override
        public ServletInputStream getInputStream() throws IOException {
            if(StrKit.isBlank(bodyJsonStr)) {
                bodyJsonStr = "";
            }
            // 必须指定utf-8编码，否则json请求数据中如果包含中文,会出现异常
            final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bodyJsonStr.getBytes("utf-8"));
            ServletInputStream servletInputStream = new ServletInputStream() {
                @Override
                public boolean isFinished() {
                    return false;
                }
                @Override
                public boolean isReady() {
                    return false;
                }
                @Override
                public void setReadListener(ReadListener readListener) {
                }
                @Override
                public int read() throws IOException {
                    return byteArrayInputStream.read();
                }
            };
            return servletInputStream;
        }

        @Override
        public BufferedReader getReader() throws IOException {
            return new BufferedReader(new InputStreamReader(this.getInputStream()));
        }

}